using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Microsoft.Office.Project.EPMSync.Config.Library;

namespace Microsoft.Office.Project.EPMSync.Service
{
    /// <summary>
    /// Corresponds to an attribute change event retrieved from the database
    /// </summary>
    public class AttributeChange
    {
        private int _id;
        private int _entityId;
        private int _attributeId;
        private int _dataType;
        private int _accountId;
        private string _projectName;
        private Guid _projectUid;
        private string _oldValue;
        private string _newValue;
        private ChangeState _changeStatus = ChangeState.Unknown;

        /// <summary>
        /// Corresponds to database column:
        /// [ID] [int] IDENTITY(1,1) NOT NULL
        /// </summary>
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[EntityID] [int] NOT NULL,
        /// </summary>
        public int EntityId
        {
            get { return _entityId; }
            set { _entityId = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[AttributeID] [int] NOT NULL
        /// </summary>
        public int AttributeId
        {
            get { return _attributeId; }
            set { _attributeId = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[DataType] [int] NOT NULL,
        /// </summary>
        public int DataType
        {
            get { return _dataType; }
            set { _dataType = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[AccountID] [int] NOT NULL
        /// </summary>
        public int AccountId
        {
            get { return _accountId; }
            set { _accountId = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[ProjectName] [string] NOT NULL
        /// </summary>
        public string ProjectName
        {
            get { return _projectName; }
            set { _projectName = value; }
        }

        /// <summary>
        /// The Project Server project UID, determined from the project name.
        /// </summary>
        public Guid ProjectUid
        {
            get { return _projectUid; }
            set { _projectUid = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[OldValue] [nvarchar(4000)] NOT NULL
        /// </summary>
        public string OldValue
        {
            get { return _oldValue; }
            set { _oldValue = value; }
        }

        /// <summary>
        /// Corresponds to database column:
        ///[NewValue] [nvarchar(4000)] NOT NULL
        /// </summary>
        public string NewValue
        {
            get { return _newValue; }
            set { _newValue = value; }
        }

        /// <summary>
        /// Track the workflow of the change being processed
        /// </summary>
        public ChangeState ChangeStatus
        {
            get { return _changeStatus; }
            set { _changeStatus = value; }
        }

        /// <summary>
        /// Constuctor which takes the data row
        /// </summary>
        /// <param name="dr">Row read from the esQUEUE_ATTRIBUTE_EVENTS table</param>
        public AttributeChange(DataRow dr)
        {
            if (dr != null)
            {
                _id = (int)dr["ID"];
                _entityId = (int)dr["EntityID"];
                _attributeId = (int)dr["AttributeID"];
                _dataType = (int)dr["DataType"];
                _accountId = (int)dr["AccountID"];
                _projectName = (string)dr["ProjectName"];
                _projectUid = Guid.Empty;
                _oldValue = (string)dr["OldValue"];
                _newValue = (string)dr["NewValue"];
                _changeStatus = ChangeState.Detected;
            }
        }

        /// <summary>
        /// Constuctor which takes all the parameters of the class.
        /// </summary>
        /// <param name="id">Identifier of the change.</param>
        /// <param name="entityID">PPS EntityID associated with the change.</param>
        /// <param name="attributeID">PPS AttributeID associated with the change.</param>
        /// <param name="dataType">Data type of the attribute changed.</param>
        /// <param name="accountID">PPS AccountID which the change is associated with.</param>
        /// <param name="projectName">The name of the project being affected.</param>
        /// <param name="projectUid">The Project Server project UID which corresponds to the Project Name</param>
        /// <param name="oldValue">The value prior to the change.</param>
        /// <param name="newValue">The new desired value.</param>
        public AttributeChange(int id, int entityID, int attributeID, int dataType, int accountID,
            string projectName, Guid projectUid, string oldValue, string newValue)
        {
            _id = id;
            _entityId = entityID;
            _attributeId = attributeID;
            _dataType = dataType;
            _accountId = accountID;
            _projectName = projectName;
            _projectUid = projectUid;
            _oldValue = oldValue;
            _newValue = newValue;
            _changeStatus = ChangeState.Detected;
        }

    }
}
